package com.hr.leetcode.哈希表;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 周恒哲
 * @date 2020/05/14
 **/
public class _242_有效的字母异位词 {
    public boolean isAnagram(String s, String t) {
        if (s.length()!=t.length()){
            return false;
        }
        char[] s1 = s.toCharArray();
        char[] t1 = t.toCharArray();
        Arrays.sort(s1);
        Arrays.sort(t1);
        return Arrays.equals(s1,t1);
    }
    public boolean isAnagram1(String s, String t) {
        if (s.length()!=t.length()){
            return false;
        }
        char[] s1 = s.toCharArray();
        char[] t1 = t.toCharArray();
        Map<Character,Integer> map = new HashMap<>();
        Map<Character,Integer> map2 = new HashMap<>();
        for ( int i=0;i<s.length();i++){
            if (map.get(s1[i])==null){
                map.put(s1[i],0);
            }else{
                map.put(s1[i],map.get(s1)+1);
            }
            if (map2.get(t1[i])==null){
                map2.put(t1[i],0);
            }else{
                map2.put(t1[i],map2.get(t1)+1);
            }
        }
      return map.equals(map2);
    }

}
